package com.bcu.wish.controller;

import com.bcu.wish.entity.Beginwish;
import com.bcu.wish.entity.Message;
import com.bcu.wish.service.BeginwishService;
import com.bcu.wish.service.TimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Api(tags = "初始志愿模块")
@RestController
@RequestMapping("/api/Beginwish")
public class BeginWishController {

    @Resource
    private BeginwishService beginwishService;

    @Resource
    private TimeService timeService;

    @PostMapping("/")
    @ApiOperation(value = "填报初始志愿")
    public Message insert(Beginwish beginwish){
        //beginwish.setBeginwishPostTime(new Date());
        Date begindate = timeService.selectByPrimaryKey(1).getObj().getTimeLimittime(); //开始初步意向时间
        Date stopdate = timeService.selectByPrimaryKey(2).getObj().getTimeLimittime();  //截止初步意向时间
        Date nowdate = new Date();
        if (nowdate.after(begindate)) {
            if(nowdate.before(stopdate)){
                beginwish.setBeginwishPostTime(nowdate);
                return beginwishService.insertSelective(beginwish);
            }
            return Message.fail("填报初始志愿时间已截止！");
        }
        return Message.fail("填报初始志愿时间未开始！");
    }

    @GetMapping("/one")
    @ApiOperation(value = "根据初始志愿id查看初始志愿")
    public Message<Beginwish> selectById(int id)
    {
        return beginwishService.selectByPrimaryKey(id);
    }

    @GetMapping("/all")
    @ApiOperation(value = "查看所有初始志愿")
    public Message<List<Beginwish>> selectAll(){
        return beginwishService.selectAll();
    }

    @ApiOperation(value = "导出所有初始志愿填报数据Excel,返回url")
    @GetMapping(value = "/Excel")
    public Message getExcel(){
        return beginwishService.getExcel();
    }

    @GetMapping("/userid/one")
    @ApiOperation(value = "根据userid查看初始志愿")
    public Message<Beginwish> selectByUserId(String userId)
    {
        return beginwishService.selectByUserId(userId);
    }

    @GetMapping("/ceshiceshiceshi")
    @ApiOperation(value = "不许连！！")
    public Message ceshi(){
        return beginwishService.ceshi();
    }

    @GetMapping("/ceshi")
    @ApiOperation(value = "123123213！")
    public Message ceshi2(){
        return beginwishService.ceshi2();
    }
}
